package com.sd.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.mobclick.android.MobclickAgent;
import com.sd.view.DialogManager;

/**
 * 
 * 主界面
 * 本地书架，在线书城，在线搜索
 * 
 * @author Alex.Lu
 * 
 */
public class MainTabActivity extends TabActivity implements OnTabChangeListener {

	private final static String TAG_LOCAL = "tag_local";
	public final static String TAG_ONLINE = "tag_online";
	private final static String TAG_SEARCH = "tag_search";

	private TabHost mTabHost;
	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		mTabHost.setCurrentTab(1);
		super.onNewIntent(intent);
	}

	private Intent mLocal;
	private Intent mOnline;
	private Intent mSreach;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_maintab);
		
		initIntent();
		
		setupTabHost();
		
		
	}


	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		if(event.getKeyCode() ==KeyEvent.KEYCODE_MENU
				&& event.getAction() == KeyEvent.ACTION_DOWN){
			DialogManager.getInstance().createSystemSet(this, null);
		}
		return super.dispatchKeyEvent(event);
	}

	
	/**
	 * 初始化意图
	 */
	private void initIntent() {
		// TODO Auto-generated method stub
		mLocal = new Intent(this, BetaReadActivity.class);
		mOnline = new Intent(this, BookStoreActivity.class);
		mSreach = new Intent(this, SreachActivity.class);
	}

	/**
	 * 初始化TAB
	 */
	private void setupTabHost() {
		this.mTabHost = getTabHost();
		
		ImageButton mLocalIB = new ImageButton(this);
		mLocalIB.setBackgroundResource(R.drawable.shelf_bottom_);
		mLocalIB.setPadding(0, 12, 0, 0);
		mLocalIB.setImageResource(R.drawable.shelf_bottom_local);
		mTabHost.setOnTabChangedListener(this);
		mTabHost.addTab(mTabHost.newTabSpec(TAG_LOCAL).setIndicator(mLocalIB)
				.setContent(mLocal));
		
		ImageButton mOnlineIB = new ImageButton(this);
		mOnlineIB.setBackgroundResource(R.drawable.shelf_bottom_);
		mOnlineIB.setPadding(0, 12, 0, 0);
		mOnlineIB.setImageResource(R.drawable.shelf_bottom_online);
		mTabHost.addTab(mTabHost.newTabSpec(TAG_ONLINE).setIndicator(mOnlineIB)
				.setContent(mOnline));
		
		ImageButton mSearchIB = new ImageButton(this);
		mSearchIB.setBackgroundResource(R.drawable.shelf_bottom_);
		mSearchIB.setImageResource(R.drawable.shelf_bottom_search);
		mSearchIB.setPadding(0, 12, 0, 0);
		mTabHost.addTab(mTabHost.newTabSpec(TAG_SEARCH).setIndicator(mSearchIB)
				.setContent(mSreach));
		
		mTabHost.setCurrentTab(0);
	}

	
	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		mTabHost.setCurrentTabByTag(tabId);
		
		MobclickAgent.onEvent(this, "onTabChanged",mTabHost.getCurrentTabTag());
	}


}
